// gjw/pages/doubao/buy.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    num:1, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
		var url = wx.getStorageSync('siteUrl')
		app.util.request({
			url: 'entry/wxapp/Isregister',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				
				if (res.data.code == 200) {
					that.get_list()
				} else {
					wx.redirectTo({
						url: '../index/register',
					})
				}
			}
		})
    that.setData({
      siteUrl: url,
      info: options,
      sum_price: Number(options.price),
      all_money: Number(options.price)+Number(options.exfee),
      yunfei: Number(options.exfee)
    })
  },

  // 判断有没有地址
  get_list: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url: 'entry/wxapp/Addresslist',
      data: {
        openid: openid
      },
      success: function (res) {
        
        if (res.data.code == 200) {
          var arry = res.data.res
          if (arry.length==1){
           that.setData({
             comdata: arry[0]
           })        
         }else{
            for (var i = 0; i < arry.length;i++){
              if (arry[i].state==1){
                that.setData({
                  comdata: arry[i]
                })
              }
            }
         }
        } else if (res.data.code == 500) {
          wx.showModal({
            title: '提示',
            content: '您暂时还没有收货地址，立刻去新建收货地址吧~',
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../order/add_area',
                })
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }

      }
    })
    
  },

  jia: function (e) {
    var that = this
    var shuliang = that.data.num
    var dj = Number(that.data.info.price)
    var yf=Number(that.data.info.exfee)
      that.setData({
        num: shuliang +1,
        sum_price: (shuliang+1)*dj,
        all_money: (shuliang + 1) * dj+yf
      })
  },


  jian:function(e){
    var that=this
    var shuliang=that.data.num
    var dj = Number(that.data.info.price) 
    var yf = Number(that.data.info.exfee)      
    if(shuliang<=1){
      that.setData({
        num:1,
        sum_price:dj        
      })
      wx.showToast({
        title: '1是最小值啦~',
        icon: 'none',
      })
    }else{
      that.setData({
        num: shuliang - 1,
        sum_price: (shuliang - 1) * dj,
        all_money: (shuliang-1) * dj + yf
                      
      })
    }
  },


  sub:function(e){
    var that=this
    var formid=e.detail.formId
    var openid = wx.getStorageSync('openid')
    var db_id = that.data.info.id
    var price = that.data.info.price
    var phone = that.data.comdata.phone
    var addressname = that.data.comdata.addressname
    var address = that.data.comdata.address + that.data.comdata.addressdetail
    var num=that.data.num
    var sum = that.data.all_money
    app.util.request({
      url:'entry/wxapp/Dborder',
      data:{
        openid: openid, formid: formid, p_id: db_id, type: 1, ofee: price, phone: phone, name: addressname, address: address, num: num, ofees:sum,
      },
      success:function(res){
        
        that.setData({
          res_id: res.data.res_id
        })
        var res_id = that.data.res_id
        if(res.data.code==200){
          app.util.request({
            url: 'entry/wxapp/Dborderpay',
            data: {
              openid: openid, res_id: res_id, ofees:sum
            },
            success:function(res){
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': 'MD5',
                'paySign': res.data.paySign,
                'success': function (res) {
                  app.util.request({
                    url: 'entry/wxapp/Dborderpaysuccess',
                    data: {
                       res_id: res_id
                    },
                    success: function (res) {
                     wx.showModal({
                       title: '提示',
                       content: '您的订单已经付款成功，可以到订单页中查看',
                       showCancel: true,
                       cancelText: '继续逛逛',
                       cancelColor: '',
                       confirmText: '我的订单',
                       confirmColor: '',
                       success: function(res) {
                         if(res.confirm){
                           wx.navigateTo({
                             url: '../order/index',
                           })
                         }else if(res.cancel){
                           wx.reLaunch({
                             url: '../index/index',
                           })
                         }
                       },
                       fail: function(res) {},
                       complete: function(res) {},
                     })
                      wx.reLaunch({
                        url: '../index/index',
                      })
                    }
                  })

                },
                'fail': function (res) {
                  ;
                  wx.showToast({
                    title: '支付失败',
                    icon: "loading",
                  })
                },
                'complete': function (res) {
                  ;
                  wx.showToast({
                    title: '取消支付',
                    icon: "loading",
                  })
                }
              })

            }
            
          })
         
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this
    var areaInfo=wx.getStorageSync('areaInfo')
    that.setData({
      comdata: areaInfo
    })
    
		app.util.request({
			url: 'entry/wxapp/Isregister',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				
				if (res.data.code == 200) {
					that.get_list()
				}
			}
		})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})